package com.ruoyi.web.controller.resturant;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.BizException;
import com.ruoyi.resturant.domain.ResDish;
import com.ruoyi.resturant.service.IResDishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Anonymous
@RestController
@RequestMapping("/api/resturant/dish")
@Api(tags="菜品管理")
public class ResDishConroller {
    @Autowired
    private IResDishService resDishService;
    @Autowired
    private RedisCache redisCache;
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "编号", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "name", value = "菜品名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "adress", value = "菜品地址", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "contact", value = "联系电话", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "cover", value = "封面", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "detail", value = "菜品详情", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "createBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "updateBy", value = "更新者", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "String", dataTypeClass = Date.class)
    })
    /**
     * 查询菜品列表
     */
    @ApiOperation("查询菜品列表")
    @ApiImplicitParam(name = "name", value = "菜品名称",
            dataType = "string", dataTypeClass = String.class)
    /**
     获取所有商户列表
     * */

    @GetMapping("/list")
    public AjaxResult list(@RequestParam(value = "name", required = false) String dishName) {
        List<ResDish> list = resDishService.selectResDishByParam(dishName);
        return AjaxResult.success(list);
    }
    /**
     * 获取菜品详细信息
     */

    @ApiOperation(value = "获取菜品详细信息", notes = "TODO：取得某个菜品的详细信息")
    @ApiImplicitParam(name = "id", value = "菜品ID",
            required = true, dataType = "long", paramType = "path",
            dataTypeClass = Long.class)
    @GetMapping(value = "/{id}")
    public AjaxResult getRedisObj(@PathVariable("id") Long id){
        // 1 根据菜品ID从redis取得菜品详情的json字符串
        String str = redisCache.getCacheObject(String.valueOf(id));
        // 2 把菜品详情的json字符串转换为一个model对象ResDish
        ResDish resDish = JSON.parseObject(str, ResDish.class);

        if (resDish == null) {
            // 3 redis不存在这个菜品的场合，从数据库里查询
            resDish = resDishService.selectResDishById(id);
            // 4 数据库里该菜品ID不存在的场合，抛出业务异常BizException
            if(resDish == null) {
                throw new BizException(404, "该菜品id("+ id + ")不存在");
            }
            String strDB = JSON.toJSONString(resDish);
            redisCache.setCacheObject(String.valueOf(id), strDB,
                    1, TimeUnit.HOURS);
        }

        // 5 返回取得model对象
        return AjaxResult.success(resDish);
    }


    /**
     * 新增菜品
     */
    @Log(title = "菜品管理",businessType = BusinessType.INSERT)

    @ApiOperation("新增菜品")
    @PostMapping
    public AjaxResult add(@RequestBody ResDish resDish) {
        return AjaxResult.success(resDishService.insertResDish(resDish));
    }


    /**
     * 修改菜品
     */
    @Log(title = "菜品管理",businessType = BusinessType.UPDATE)

    @ApiOperation("修改菜品")
    @PutMapping
    public AjaxResult edit(@RequestBody ResDish resDish) {
        //1先根据菜品ID查询数据库
        ResDish resDishDB=  resDishService.selectResDishById(resDish.getId());
        //如果该菜品不存在，就抛出业务异常
        if (resDishDB == null){
            throw  new BizException(404,"该菜品id"+resDish.getId()+"不存在");
        }
        //如果菜品存在，执行更新操作
        return AjaxResult.success(resDishService.updateResDish(resDish));
    }

    /**
     * 删除菜品
     */
    @Log(title = "菜品管理",businessType = BusinessType.DELETE)

    @ApiOperation("删除菜品")
    @ApiImplicitParam(name = "id", value = "菜品ID", required = true, dataType = "long", paramType = "path",
            dataTypeClass = Long.class)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return AjaxResult.success(resDishService.deleteResDishById(id));
    }
}
